<?php require_once('./header.php');
    
    if (!isset($_GET['ct'])) {
        
        // Convert News
        $dir = './storage/news/';
        $dirlist = opendir($dir);
        $files = array();
        while( ($file = readdir($dirlist)) !== false) {
            if (!strpos($file,'~')) {
                if(!is_dir($file)) {
                    $files[] = $file;
                }
            }
        }
        
        $delete = array();
        
        $newsdb = load_db('news');
        $tens = 0;
        while (in_array((($tens*10)+1).'-'.(($tens+1)*10),$files)) {
            $f = file('./storage/news/'.(($tens*10)+1).'-'.(($tens+1)*10));
            
            for ($i=0;$i<10;$i++) {
                $line = rtrim($f[$i]);
                if (!$line) {
                    break;
                } elseif (!strpos($line,'|')) {
                    $delete[] = $newsdb->save(array('title'=>'Deleted','post'=>'This post should be automatically deleted','timestamp'=>date('U'),'author'=>1));
                } else {
                    $news = array();
                    $news['timestamp'] = substr($line,0,8)*60;
                    list($title,$user,$post) = explode('|',rtrim(substr($line,8)));
                    $news['title'] = destorify($title);
                    $news['post'] = destorify($post);
                    $news['author'] = $user;
                    $newsdb->save($news);
                }
                
                //unlink('../storage/news/'.(($tens*10)+1).'-'.(($tens+1)*10));
            }
            
            $tens++;
        }
        
        foreach ($delete as $id) $newsdb->delete($id);
        
        
        // Convert users
        $userdb = load_db('user');
        
        $ufile = file('./storage/private/users.php');
        array_shift($ufile);
        foreach ($ufile as $uline) {
            $spliced = explode('|',rtrim($uline));
            $user = array(
              'group' => substr($spliced[0],13),
              'session' => '',
              'name' => destorify($spliced[1]),
              'password' => $spliced[2],
            );
            $userdb->save($user);
        }
        
        
        
        
        
?>
        <h2>Oh, just one more thing</h2>
        <p>We now have two fields for comics: tagline and blurb. The tagline is a one line phrase used by default in the hover text of your comic, the blurb is a richtext description to go underneath a comic.</p>
        <p>Blurbs in earlier versions of ComicCMS were just one line phrases displayed under the comic, so:</p>
        <p>Would you like to use your old blurbs as taglines? Then <a href="?ct=1">click here</a></p>
        <p>Or would you like to use your old blurbs as blurbs still? Then <a href="?ct=2">click here</a></p>
    </div>
</body>
</html>
<?php
        exit;
    } else {
        // Convert Comics
        $dir = './storage/comics/';
        $dirlist = opendir($dir);
        $files = array();
        while( ($file = readdir($dirlist)) !== false) {
            if (!strpos($file,'~')) {
                if(!is_dir($file)) {
                    $files[] = $file;
                }
            }
        }
        
        
        $comicdb = load_db('comic');
        $id = 1;
        while (in_array($comicdb->rangename($id,100),$files)) {
            $f = file('./storage/comics/'.$comicdb->rangename($id,100));
            
            foreach ($f as $k => $line) {
                $comic = array();
                $comic['timestamp'] = substr($line,0,8)*60;
                
                if ($v == '0.1.5b') {
                    list($title,$user,$ext,$news) = explode('|',rtrim(substr($line,8)));
                    $blurb = '';
                } else {
                    list($title,$user,$ext,$blurb,$news) = explode('|',rtrim(substr($line,8)));
                }
                
                $comic['title'] = destorify($title);
                $comic['ext'] = destorify($ext);
                $comic['blurb'] = destorify($blurb);
                $comic['tagline'] = destorify($blurb);
                $comic['author'] = $user;
                
                $lower = (strpos($news,'-')?substr($news,0,strpos($news,'-')):$news);
                $upper = (strpos($news,'-')?substr($news,strpos($news,'-')+1):$news);
                $comic['news'] = range($lower,$upper);
                
                if ($_GET['ct']==2) {
                    $comic['tagline'] = '';
                } else {
                    $comic['blurb'] = '';
                }
                
                @$info = getimagesize('../img/comic/'.($id+$k).'.'.$comic['ext']);
                $comic['width'] = ($info?$info[0]:'');
                $comic['height'] = ($info?$info[1]:'');
                
                //copy('../admin/img/nothumb.jpg','../admin/img/thumb/'.($id+$k).'.jpg');
                
                $comicdb->save($comic);
            }
            
            $id += 100;
        }
    }
    
    $ufile = file('./storage/private/usergroups.php');
    array_shift($ufile);
    array_shift($ufile);
    $usergroups = array();
    foreach ($ufile as $uline) {
        $usergroups[] = substr($uline,0,strpos($uline,'|'));
    }
    
    $perms = array('Log In','Manage comics','Manage News','Manage Plugins','Manage Site Templates','Manage Users','Manage Usergroups','Edit Settings','Apply Updates','Uninstall ComicCMS');
?>
        <h1>Comics, News &amp; Users converted</h1>
        <p>There, now wasn't that easy <img src="../img/smileys/smile.gif" alt=":)" /></p>
        <h2>Now on to your usergroups</h2>
        <p>We need to set the permissions for your usergroups. This is a simplified table of the permissions possible, once you've finished the upgrade you can go to the edit usergroup page and tweak it more finely.</p>
        <form action="step2.php" method="post">
            <table>
                <tr><td></td><?php foreach ($usergroups as $uid => $g) echo '<th>'.$g.'</th>'; ?>
<?php foreach ($perms as $id => $p) { ?>
                <tr><th><?php echo $p; ?></th><?php foreach ($usergroups as $uid => $g) echo '<td><input type="checkbox" name="'.$id.'_'.$uid.'" '.($uid==0?'checked="checked" ':'').'/></td>'; ?>
<?php } ?>
            </table>
            <input type="submit" name="doperms" value="Use these permissions" />
        </form>
    </div>
</body>
</html>